package com.yloveg.impl;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import com.yloveg.service.MailUtil;

import freemarker.template.Template;
import freemarker.template.TemplateException;
 
/**
 * @Description:发邮件
 * @author: yangkai
 */
 
@Service
public class MailUtilImpl implements MailUtil{
 
    private static final Logger logger = LoggerFactory.getLogger(MailUtilImpl.class);
 
	@Resource
	JavaMailSender mailSender;
	@Resource
	SimpleMailMessage simpleMailMessage;
	@Resource
    private FreeMarkerConfigurer fmc;

 
	/**
	 * 
	 * @Description:普通文本发邮件形式
	 * @param subject 主题
	 * @param content 正文
	 * @param toMail 收件人邮箱
	 * void
	 * @exception:
	 * @author: yangkai
	 * @time:2017年4月20日 下午8:06:05
	 */
	@Override
	public void sendSimpleMail(String subject,String content,String toMail) {
    	simpleMailMessage.setSubject(subject);
    	simpleMailMessage.setText(content);
    	simpleMailMessage.setTo(toMail);
    	mailSender.send(simpleMailMessage);
    	logger.info("邮件发送成功.."); 
	}
	
	/**
	 * 
	 * @Description:html发邮件形式
	 * @param subject 主题
	 * @param content 正文
	 * @param toMail 收件人邮箱
	 * void
	 * @exception:
	 * @author: yangkai
	 */
	public void sendHtmlMail(String subject,String content,String toMail) {
		MimeMessage mailMessage = mailSender.createMimeMessage(); 
		MimeMessageHelper messageHelper;
	    try {		    	
	    	messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");
	        messageHelper.setFrom(simpleMailMessage.getFrom());
			messageHelper.setTo(toMail);
			messageHelper.setSubject(subject); 
			messageHelper.setText("<html><head></head><body><h1>"+content+"</h1></body></html>",true);
			mailSender.send(mailMessage); 
		    logger.info("邮件发送成功.."); 
		} catch (MessagingException e) {
			e.printStackTrace();
		} 
	}
	
	/**
	 * 
	 * @Description: 带图片发邮件形式
	 * @param subject 主题
	 * @param content 正文
	 * @param toMail 收件人邮箱
	 * @param picturePath 图片路径
	 * void
	 * @exception:
	 * @author: yangkai
	 */
	public void sendPictureMail(String subject,String content,String toMail,String picturePath){
	    MimeMessage mailMessage = mailSender.createMimeMessage(); 
	    MimeMessageHelper messageHelper;
		try {
			messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");
			messageHelper.setFrom(simpleMailMessage.getFrom());
			messageHelper.setTo(toMail);
			messageHelper.setSubject(subject); 
			//messageHelper.setText("<html><head></head><body><h1>" + content +"</h1><img src=/"+cid:aaa + /"/></body></html>",true); 
			FileSystemResource img = new FileSystemResource(new File(picturePath)); 
			messageHelper.addInline("aaa",img); 
			//发送邮件 
			mailSender.send(mailMessage);
			logger.info("邮件发送成功.."); 
		} catch (MessagingException e) {
			e.printStackTrace();
		} 
	}
	
	/**
	 * 
	 * @Description:带附件发邮件形式
	 * @param subject 标题
	 * @param content 正文
	 * @param toMail 收件人邮箱
	 * @param accessoryPath 附件路径
	 * @param accessoryName 附件名
	 * void
	 * @exception:
	 * @author: yangkai
	 */
	public void sendMailTakeAccessory(String subject,String content,String toMail,String accessoryPath,String accessoryName){
	    MimeMessage mailMessage = mailSender.createMimeMessage(); 
	    MimeMessageHelper messageHelper;
		try {
			messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");
			messageHelper.setFrom(simpleMailMessage.getFrom());
		    messageHelper.setTo(toMail);
			messageHelper.setSubject(subject); 
		    messageHelper.setText("<html><head></head><body><h1>"+content+"</h1></body></html>",true); 
		    FileSystemResource file = new FileSystemResource(new File(accessoryPath)); 
		    messageHelper.addAttachment(accessoryName,file);
		    mailSender.send(mailMessage); 
		    logger.info("邮件发送成功.."); 
		} catch (MessagingException e) {
			e.printStackTrace();
		} 
	
	}
	
	/**
	 * 
	 * @Description:根据模板发邮件形式
	 * @param subject 主题
	 * @param map 模板内容
	 * @param template 模板名称
	 * @param toMail 收件人邮箱
	 * void
	 * @exception:
	 * @author: yangkai
	 * @throws IOException 
	 * @throws TemplateException 
	 */
	public void sendTemplateMail(Map<String, Object> map,String subject,String template,String toMail) throws IOException, TemplateException{
		MimeMessage mailMessage = mailSender.createMimeMessage(); 
		MimeMessageHelper messageHelper;
	    try {		    	
	    	messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");
	        messageHelper.setFrom(simpleMailMessage.getFrom());
			messageHelper.setTo(toMail);
			messageHelper.setSubject(subject);
			String path = "template/"+template;
			Template t =  fmc.getConfiguration().getTemplate(path);//获取模板
            String haha = FreeMarkerTemplateUtils.processTemplateIntoString(t, map);//解析模板
			messageHelper.setText(haha,true);
			mailSender.send(mailMessage); 
		    logger.info("邮件发送成功.."); 
		} catch (MessagingException e) {
			e.printStackTrace();
		} 
	}
	
}
